package tushare;

import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 常胜将军
 *
 * 将筹码分成10份,
 * 每胜利一次就追加一份筹码,
 * 每失败一次就减少一份筹码,
 * 最多投入10份,
 * 最少投入1份
 */
@lombok.Data
public class EverVictoriousGeneral implements Tactic{

    /**
     * 总价值
     */
    double total;

    /**
     * 持仓
     */
    double position;

    /**
     * 持币
     */
    double coins;

    /**
     * 股票数量
     */
    double number;

    /**
     * 价格
     */
    double price;

    @Override
    public String toString() {
        return "EverVictoriousGeneral{" +
                "total=" + total +
                ", position=" + position +
                ", coins=" + coins +
                ", number=" + number +
                ", price=" + price +
                '}';
    }

    @Override
    public EverVictoriousGeneral execute(double price) {
        EverVictoriousGeneral res = new EverVictoriousGeneral();
        res.price = price;
        res.total = price * number + coins;
        res.coins = coins;
        res.position = position;
        res.number = number;

        double spice = res.total / 10;
        double n = spice / price;

        if(price > this.price){
            System.out.printf("买入,%2f=>%2f \r\n",this.price,price);
            if(coins > spice){
                res.position = position + spice;
                res.coins = coins - spice;
                res.number = number + n;
            }else {
                System.out.printf("不够买出,coins=%2f \n",this.coins);
            }
        }else {
            System.out.printf("卖出,%2f=>%2f \r\n",this.price,price);
            if(position > spice * 2){
                res.position = position - spice;
                res.coins = coins + spice;
                res.number = number - n;
            }else {
                System.out.printf("不够卖出,position=%2f \n",this.position);
            }
        }

        return res;
    }
}
